import sqlite3


def get_all_conn_data():
    # 创建并连接数据库
    conn = sqlite3.connect('./db/lcls.db')
    # 创建游标对象
    cursor = conn.cursor()
    # 查询语句
    query = "SELECT * FROM all_conn"
    # 执行SQL语句来查询所有ssh连接
    cursor.execute(query)
    result = cursor.fetchall()
    # 提交更改并关闭连接
    conn.commit()
    conn.close()
    return (len(result), result)

def add_one_conn(obj) -> int:
    '''
        本函数会返回插入数据库后影响的行数
        不出意外的话都是 1
    '''
    # 创建并连接数据库
    conn = sqlite3.connect('./db/lcls.db')
    # 创建游标对象
    cursor = conn.cursor()
    # 查询语句
    query = f"INSERT INTO all_conn(conn_name, user, password, Ip, Port) VALUES (\'{obj['conn_name']}\', \'{obj['user']}\', \'{obj['password']}\', \'{obj['ip']}\', {obj['port']})"
    # 执行SQL语句来查询所有ssh连接
    cursor.execute(query)
    # 提交更改并关闭连接
    conn.commit()
    rows_affected = cursor.rowcount
    conn.close()
    return rows_affected

